import pytz
from django.conf import settings
from django.shortcuts import render
from django.utils import timezone

from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet
from rest_framework.generics import ListAPIView

from goods.models import GoodsVisitCount
from orders.models import OrderInfo
from users.models import User
from meiduo_admin.serializers.home_serializer import GoodsVisitCountSerializer
from datetime import datetime, timedelta


class HomeView(ViewSet):
    #   用户总量统计
    @action(methods=['get'], detail=False)
    def total_count(self, request):
        count = User.objects.count()
        date = timezone.now().date()
        return Response({
            'count': count,
            'date': date
        })

    # 日增用户统计
    @action(methods=['get'], detail=False)
    def day_increment(self, request):
        cur_time = timezone.now()
        local_0_time = cur_time.astimezone(tz=pytz.timezone(settings.TIME_ZONE))
        shanghai_0_time = local_0_time.replace(hour=0,minute=0,second=0, microsecond=0)
        count = User.objects.filter(date_joined__gte=shanghai_0_time).count()
        return Response({
            'count':count,
            'date':shanghai_0_time
        })

    # 日活跃用户统计
    @action(methods=['get'], detail=False)
    def day_active(self, request):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE))\
            .replace(hour=0, minute=0, second=0, microsecond=0)
        count = User.objects.filter(last_login__gte=local_0_time).count()
        return Response({
            'count': count,
            'date':local_0_time.date()
        })

    #  日下单用户量统计
    @action(methods=['get'], detail=False)
    def day_orders(self, request):
        local_0_time = timezone.now().astimezone(tz=pytz.timezone(settings.TIME_ZONE))\
            .replace(hour=0, minute=0, second=0, microsecond=0)
        user_queryset = User.objects.filter(orderinfo__create_time__gte=local_0_time)
        count = len(set(user_queryset))

        return Response({
            'count': count,
            'date': local_0_time.date()
        })


    @action(methods=['get'], detail=False)
    def month_increment(self, request):
        cur_0_time = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE)) \
            .replace(hour=0, second=0, minute=0, microsecond=0)
        begin_0_time = cur_0_time - timedelta(days=29)

        calc_list = []
        for index in range(30):
            # calc_0_time：30天中的某一天
            calc_0_time = begin_0_time + timedelta(days=index)

            count = User.objects.filter(date_joined__gte=calc_0_time,
                                        date_joined__lt=calc_0_time + timedelta(days=1)).count()

            calc_list.append({
                "count": count,
                "date": calc_0_time.date()
            })

        return Response(calc_list)


class GoodsVisitCountView(ListAPIView):
    permission_classes = [IsAdminUser]
    queryset = GoodsVisitCount.objects.all()
    serializer_class = GoodsVisitCountSerializer

    def get_queryset(self):
        cur_0_time = timezone.now().astimezone(pytz.timezone(settings.TIME_ZONE))\
            .replace(hour=0, minute=0, second=0, microsecond=0)
        return self.queryset.filter(create_time__gte=cur_0_time)


